package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.Department;
import com.wgchao.identityrightsmanagement.domain.organization.repository.DepartmentRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.DepartmentConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseDepartment;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseDepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: DepartmentDao
 * @author: wgchao
 * @createTime: 2020/6/15 5:31 PM
 * @Description: TODO
 */
@Component
public class DepartmentDao implements DepartmentRepository {
    @Autowired
    private SysBaseDepartmentRepository sysBaseDepartmentRepository;
    @Autowired
    private DepartmentConvert departmentConvert;

    @Override
    public String generateCode(String agencyCode) {
        return agencyCode.substring(0, 4)+UUID.randomUUID().toString().split("-")[0].toUpperCase();
    }

    @Override
    public Department findById(String s) {
        SysBaseDepartment sysBaseDepartment = sysBaseDepartmentRepository.findFirstByCodeAndDelIsFalse(s);
        if(sysBaseDepartment == null){
            return null;
        }
        return departmentConvert.convert(sysBaseDepartment);
    }

    @Override
    public void remove(Department department, String operator) {
        SysBaseDepartment sysBaseDepartment = sysBaseDepartmentRepository.findFirstByCodeAndDelIsFalse(department.getCode());
        if(sysBaseDepartment == null){
            throw new ObjectNotExistException("部门不存在");
        }
        sysBaseDepartment.setDel(true);
        sysBaseDepartment.setLastUpdateTime(new Date());
        sysBaseDepartment.setLastUpdator(operator);
        sysBaseDepartment = sysBaseDepartmentRepository.save(sysBaseDepartment);
    }

    @Override
    public void add(Department department, String operator) {
        if(department.getId().compareTo(0L) < 0){//新增
            SysBaseDepartment sysBaseDepartment = sysBaseDepartmentRepository.findFirstByCodeAndDelIsFalse(department.getCode());
            if(sysBaseDepartment != null){
                throw new ObjectExistException("部门编号已被使用");
            }
            sysBaseDepartment = departmentConvert.convert(department);
            sysBaseDepartment.setId(null);
            sysBaseDepartment.setDel(false);
            sysBaseDepartment.setCreateTime(new Date());
            sysBaseDepartment.setCreator(operator);
            sysBaseDepartment = sysBaseDepartmentRepository.save(sysBaseDepartment);
        }else{//修改
            SysBaseDepartment sysBaseDepartment_old = sysBaseDepartmentRepository.findFirstByCodeAndDelIsFalse(department.getCode());
            if(sysBaseDepartment_old == null){
                throw new ObjectNotExistException("部门不存在");
            }
            SysBaseDepartment sysBaseDepartment = departmentConvert.convert(department);
            sysBaseDepartment.setId(sysBaseDepartment_old.getId());
            sysBaseDepartment.setLastUpdator(operator);
            sysBaseDepartment.setLastUpdateTime(new Date());
            sysBaseDepartment.setCreateTime(sysBaseDepartment_old.getCreateTime());
            sysBaseDepartment.setCreator(sysBaseDepartment_old.getCreator());
            sysBaseDepartment.setDel(sysBaseDepartment_old.getDel());
            sysBaseDepartment = sysBaseDepartmentRepository.save(sysBaseDepartment);
        }
    }

    @Override
    public void addAll(List<Department> departments, String operator) {
        for(Department department : departments){
            add(department, operator);
        }
    }

    @Override
    public void removeAll(List<Department> departments, String operator) {
        for(Department department : departments){
            remove(department, operator);
        }
    }
}
